package com.product.controller;

import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.product.entity.CategoryEntity;
import com.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品三级分类
 *
 * @author
 * @email 3299686223@qq.com
 * @date 2022-08-08 14:42:26
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions(":category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions(":category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions(":category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":category:delete")
    public R delete(@RequestBody List<Long> catIds) {
        categoryService.removeByIds(catIds);

        return R.ok();
    }

    /**
     * 获取商品三级结构
     *
     * @return 数据
     */
    @GetMapping("/list/tree")
    public R getTree() {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }

    /**
     * 排序
     *
     * @param category
     * @return 结果
     */
    @PostMapping("/update/sort")
    public R updateSort(@RequestBody List<CategoryEntity> category) {
        categoryService.updateBatchById(category);
        return R.ok();
    }
}
